import request from '@/api/axios'
import {AxiosPromise} from 'axios'
import {DataRecordList, DataRes, DictItemData} from '@/api/config'
import {Operation} from '@/components/dTable/dTable'

export type MenuType = 1 | 2

export interface pageData {
  page?: number // 默认页码
  limit?: number // 默认每页条数
}

export interface SearchDates extends pageData {
  dictCode: string // 字典编码
  dictName: string // 字典名称
  dictType?: string // 字典类型
  typeCode?: string
  operation?: Array<Operation>
}

export interface SeleteData {
  dictCode?: string
  dictItemCode?: string
  dictItemName?: string
  dictName?: string
}

export interface SeleteDatas extends SeleteData {
  label: string // 下拉中使用的分类名称
  value: string | number // 下拉中使用value值
}

export interface dictItem {
  id: number | string
  itemId?: string | number
  code?: string
}

export interface DictData {
  classIndex?: number | string
  dictIndex?: number | string
  dictItemCode?: string
  dictItemName?: string
  dictCode: string // 字典编码
  dictName: string // 字典名称
  typeCode: string //字典分类编码
  dictType: string // 字典类型
  id?: string // id
  name?:string
  pid?:string
}

export interface addDictItemDatas {
  dictId?: string // 字典编码
  // dictItemName: string // 字典名称
  // englishName: string //字典项英文名称
  // dictItemCode: string //字典分类编码
  sort?: number // 字典类型
  operation?: Array<Operation>
  dictCode?: string

  id:string
  name:string
}

export interface DictItem{
  dictItemCode?: string
  dictItemName?: string
  sort:number
}

export interface SaveDictItemDatas {
  id:string
  items:Array<DictItem>
}

export interface DictType extends SeleteDatas {
  dictItemName: string // 分类名称
  dictItemCode: string // 分类code
  dictCode?: string // 类别
}

export interface RemoveData {
  id: string
  itemId: string | number
  code?: string
}

export interface ClassData {
  dictItemCode?: string
  dictItemName?: string
  value?: number
  label?: string | number
}

export interface ClassDataT {
  items: Array<ClassData>
}

//搜索
export interface SearchRoleFromData {
  dictCode: string
  dictName: string
  typeCode: string
}

//字典分页查询
export const getDictionaries = (
  params: any
): AxiosPromise<any> => {
  return request({
    url: '/api/category/list',
    method: 'get',
    params
  })
}

//根据字典父ID查询\展示当前字典下所属字典子项明细
export const getDictItem = (
  params: dictItem
): AxiosPromise<DataRes<Array<any>>> => {
  return request({
    url: '/api/category/' + params.id,
    method: 'get'
  })
}

//根据ID删除字典信息项
export const delDict = (id: any): AxiosPromise<DataRes<null>> => {
  return request({
    url:'/api/category/' + id,
    method: 'DELETE'
  })
}

//新增字典管理记录
export const addDict = (data: any): AxiosPromise<DataRes<null>> => {
  return request({
    url: '/api/category',
    method: 'post',
    data
  })
}

//编辑字典管理记录
export const updateDictItem = (data: any): AxiosPromise<DataRes<null>> => {
  return request({
    url: '/api/category',
    method: 'put',
    data
  })
}

//配置字典新增字典子项
export const addDictItem = (
  data:any
): AxiosPromise<DataRes<null>> => {
  return request({
    url: '/api/category',
    method: 'put',
    data
  })
}

//获取下拉字典分类和类型数据
export const getDictType = (
  data: []
): AxiosPromise<DataRes<Array<DictType>>> => {
  return request({
    url: '/api/dict/get/type',
    method: 'get',
    data
  })
}

//根据前端传入ID删除配置字典子项数据
export const removeItem = (
  data: RemoveData
): AxiosPromise<DataRes<undefined>> => {
  return request({
    url:
      '/api/dict/remove/item/' +
      data.itemId +
      '/' +
      data.id +
      '/?diteCode=' +
      data.code,
    method: 'get'
  })
}

//查询分类下拉信息
export const getDict = (
  params: DictItemData
): AxiosPromise<DataRes<Array<ClassDataT>>> => {
  return request({
    url: '/api/dict/get/items',
    method: 'get',
    params
  })
}
